<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>轮播图管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="demo" class="layui-hide" lay-filter="test"></table>
            <blockquote style="margin-top: 20px" class="layui-elem-quote">效果预览</blockquote>
            <div class="layui-carousel" id="test1">
                <div carousel-item >
                    <div th:each="ban : ${banner}"><img style="height: 100%;width: 100%" th:src="${ban.url}"> </div>
             </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="imgTpl">
    <input type="hidden" id="src" value={{ d.url }}>
    <img style="display: inline-block; width: 100%; height: 100%;" src={{ d.url }}>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn" lay-event="add">新增</button>
        <button class="layui-btn layui-btn-danger " lay-event="delAll">批量删除</button>
    </div>
</script>
<script type="text/html" id="checkboxTpl">
    <input type="checkbox" name="lock" lay-skin="switch" value="{{d.ishow}}" disabled lay-text="ON|OFF" {{ d.ishow == 1 ? 'checked' : '' }} >
</script>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['laydate', 'layer', 'table','carousel'], function () {
        var $ = layui.$,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table,
            carousel = layui.carousel,
            layer = layui.layer;
        carousel.render({
            elem: '#test1'
            ,width: '50%' //设置容器宽度
            ,arrow: 'always' //始终显示箭头
            ,height: 400
        });

        //表格初始化
        table.render({
            elem: '#demo',
            id: 'idTest',
            url: '/banner/getBanners',
            page: true,
            toolbar: '#toolbarDemo',
            height: 'full-130',
            cols: [
                [
                {type: 'checkbox', fixed: 'left'},
                {
                    field: 'id',
                    title: 'ID',
                    width: 60,
                    sort: true
                }, {
                    field: 'img',
                    title: '图片',
                    width: 80,
                    templet: "#imgTpl",
                    event: 'show'
                },{
                    field: 'url',
                    title: '广告地址',
                    width: 250
                }, {
                    field: 'desc',
                    title: '描述',
                    width: 100
                }, {
                    field: 'ishow',
                    title: '是否展示',
                    width: 140,
                    templet: '#checkboxTpl',
                    unresize: true
                }, {
                    fixed: 'right', title: '操作', toolbar: '#barDemo', width: 160
                }]
            ]
        });
        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , checkData = checkStatus.data,
                formData = [];
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加轮播图'
                        , content: '/addBannerUI'
                        , maxmin: true
                        , area: ['700px', '450px']
                        , btn: ['保存', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'addAdmin'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                $.ajax({
                                    type: "post",
                                    contentType: 'application/json',
                                    url: "/addBanner",
                                    data: JSON.stringify(field),
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.code == 200) {
                                            layer.msg('添加成功',
                                                {
                                                    icon: 1,
                                                    time: 1000
                                                }, function () {
                                                    layer.close(index); //关闭弹层
                                                    window.location.reload();
                                                });
                                        } else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
                case 'delAll':
                    if (checkData.length == 0) {
                        layer.msg("不能为空");
                    } else {
                        layer.confirm('是否删除此记录?', {icon: 3, title: '提示'}, function (index) {
                            for (var i = 0; i < checkData.length; i++) {
                                formData.push(checkData[i].id);
                            }
                            $.ajax({
                                type: "delete",
                                contentType: 'application/json',
                                url: "/delBanner/" + formData,
                                data: {},
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.msg('删除成功',
                                            {
                                                icon: 1,
                                                time: 1000
                                            }, function () {
                                                layer.close(index); //关闭弹层
                                                window.location.reload();
                                            });
                                    } else {
                                        layer.msg("后台出错");
                                    }
                                }
                            });
                        });
                    }
                    break;
            }
            ;
        });
        //监听工具条
        //监听事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '编辑轮播图'
                        , content: '/editBannerUI/' + data.id
                        , maxmin: true
                        , area: ['700px', '450px']
                        , btn: ['保存', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'addAdmin'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                $.ajax({
                                    type: "put",
                                    contentType: 'application/json',
                                    url: "/banner/updateBanner",
                                    data: JSON.stringify(field),
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.code == 200) {
                                            layer.msg('更新成功',
                                                {
                                                    icon: 1,
                                                    time: 1000
                                                }, function () {
                                                    layer.close(index); //关闭弹层
                                                    window.location.reload();
                                                });
                                        } else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('是否删除此记录?', {icon: 3, title: '提示'}, function (index) {
                        $.ajax({
                            type: "delete",
                            contentType: 'application/json',
                            url: "/delBanner/" + data.id,
                            data: {},
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.msg('删除成功',
                                        {
                                            icon: 1,
                                            time: 1000
                                        }, function () {
                                            layer.close(index); //关闭弹层
                                            window.location.reload();
                                        });
                                } else {
                                    layer.msg("后台出错");
                                }
                            }
                        });
                    });
                    break;
                case 'show':
                    layer.photos(
                        {photos: {title: "查看头像", data: [{src: data.url}]}, shade: .01, closeBtn: 1, anim: 5}
                    );
                    break;
            }
            ;
        });
    });
</script>
</body>
</html>
